<?php

namespace App\Console\Commands\Tasks;
use App\Services\Tools\PushService;
use App\Services\Tools\RedisService;
use Illuminate\Console\Command;
use App\Http\Controllers\Common\TaskController;

class ConnectPushCommand extends Command
{
    //命令名
    protected $signature = 'task:connect_push';

    //命令描述：未处理的关系推送
    protected $description = 'connectPush';

    public function __construct()
    {
        parent::__construct();
    }

    //业务处理
    public function handle()
    {
        //1次/min
        $list = app('model_connect_apply')->where([
            'status' => 0
        ])->where([
            ['created_at', '>=', date('Y-m-d H:i:s',time()-86400)],
            ['created_at', '<=', date('Y-m-d H:i:s',time()-60)],
        ])->orderBy('id','desc')->get();
        $list = empty($list)?[]:$list->toArray();
        $giftIds = getArrayColumn($list,'buy_record_gift_id');
        if(!empty($giftIds)){
            $giftList = app('model_gift')->whereIn("id", $giftIds)->get();
            $giftList = empty($giftList)?[]:$giftList->toArray();
            $giftList = indexToAssoc($giftList,'id');
        }else{
            $giftList = [];
        }
        var_dump($list);
        foreach ($list as $k=>$v){
            $userData = RedisService::getUserBaseInfo($v['user_id'],'',true);
            $roomId = RedisService::getUserInRoom($v['to_user_id'])??0;
            $data = [
                'user_id' =>   $v['user_id'],
                'user_nickname' =>   $userData['nickname'],
                'user_head_picture' =>   $userData['head_picture'],
                'to_user_id' =>   $v['to_user_id'],
                'room_id' => $roomId,
                'gift_id' =>   $v['buy_record_gift_id'],
                'gift_info' =>  $giftList[$v['buy_record_gift_id']]??[],
                'apply_id' =>   $v['id'],
                'apply_type' =>   $v['type'],
            ];
            (new PushService($v['to_user_id'], $roomId))->applyConnect($data);
            return true;
        }
    }

}